package com.project.nimsen.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.project.jdbc.dao.IBaseJdbcDao;
import com.project.jdbc.service.BaseServiceImpl;
import com.project.jdbc.utils.Finder;
import com.project.nimsen.dao.IBaseDao;
import com.project.nimsen.dto.RoleDto;
import com.project.nimsen.entity.Role;
import com.project.nimsen.service.IRoleService;
@Repository("roleService")
public class RoleServiceImpl extends BaseServiceImpl implements IRoleService {
	@Resource
	IBaseDao baseDao;
	protected IBaseJdbcDao getBaseDao() {
		return baseDao;
	}
	public Role findById(String id) throws Exception {
		if(StringUtils.isBlank(id))
			return null;
		Finder finder = new Finder("select * from role where active=0 and id=:id");
		finder.setParam("id", id);
		List<Role> roles = baseDao.queryForList(finder, Role.class);
		if(CollectionUtils.isEmpty(roles))
			return null;
		return roles.get(0);
	}
	public boolean deleteById(String id) throws Exception {
		if(StringUtils.isBlank(id))
			return false;
		Finder finder = new Finder("update role set active=1 where active=0 and id=:id");
		finder.setParam("id", id);
		int count = baseDao.update(finder);
		if(count>0)
			return true;
		return false;
	}
	public Map<String, Object> findVilidate(RoleDto dto) throws Exception {
		if(dto==null)
			return null;
		Finder finder = new Finder("select id from role where active!=1");
		finder.append(" and ").append(dto.getValifield()).append("=:filed");
		finder.setParam("filed", dto.getValivalue());
		//如果id存在表示为修改
		if(StringUtils.isNotBlank(dto.getId())){
			finder.append(" and id!=:id");
			finder.setParam("id", dto.getId());
		}
		return baseDao.queryForObject(finder);
	}
	public List<Map<String, Object>> findByDto(RoleDto dto) throws Exception {
		if(dto==null)
			return null;
		Finder finder = new Finder("select * from role where active!=1");
		finder.append(" order by createTime desc");
		return baseDao.queryForList(finder, dto.getPage());
	}

}
